//Event Study Models
eststo clear

if("`c(os)'"=="Windows"){
	local tableSaveDir = "Tables\ES\"
	local plotSaveDir = "Plots\ES\"
	local pythonSaveDir = "PythonScripts\"
}
else{
	local tableSaveDir = "Tables/ES/"
	local plotSaveDir = "Plots/ES/"
	local pythonSaveDir = "PythonScripts/"
}

//Establish Control Variable Sets
local spec1Controls = "asinhMetroPop asinhNonMetroPop asinhMetroInc asinhNonMetroInc logrealmeangasprice logemployment loglicenseddrivers logrealstategdp logroadmileage"
local spec2Controls = "`spec1Controls' i.transactionDataDummy"
local spec3Controls = "`spec2Controls' i.regDummyExtSpec3"
local spec4Controls = "`spec2Controls' i.regDummyExtSpec4"

//Establish Cluster Variable
local clusterVar = "stateGroup year"

//Absorb Vars are always time and state fixed effects
local absorbVars = "ib51.stateGroup ib1995.year"

//Event Study Table format
local fmtEventStudy = `"cells(b(fmt(3) star) se(par fmt(3))) keep(*boundRelYear*) nocon label compress longtable mtitles("\makecell{No \\ DSCs}" "\makecell{Documented \\ DSCs}" "\makecell{Suspected \\ DSCs}" "\makecell{Potential \\ DSCs}") starlevels(* 0.1 ** 0.05 *** 0.01) addnotes("* p \textless 0.1, ** p \textless 0.05, *** p \textless 0.01" "Note: All models include state and year fixed effects as well as state-level time trends." "All models cluster standard errors  by state.")"'


//Loop through the number of dummies +- one year prior to the event
foreach bound of num 6 11{
	
	preserve
	
	//Add Dummy Variables for Event Study
	//Define Bounds
	local lowerBound = -`bound'
	local upperBound = `bound'
	
	//Define the relative time period that we will use
	local relYear = -1

	//Determine the dummy index of the relative year
	local relIndex = 1-`lowerBound'+`relYear'
	
	//Generate New Variable
	gen boundRelYear = yearrelativeend
	//Enforce Bounds
	replace boundRelYear = `lowerBound' if boundRelYear<=`lowerBound'
	replace boundRelYear = `upperBound' if boundRelYear>=`upperBound'
	replace boundRelYear = . if yearrelativeend==.
	
	//Create shifted Bounds
	gen shiftedBoundRelYear = boundRelYear - `lowerBound'+1
	replace shiftedBoundRelYear = `relIndex' if boundRelYear==.



	//Create Dummies
	qui tab boundRelYear, gen(boundRelYear)
	//Figure out which index for the dummy variables is the last one
	local finalInd = `upperBound'-`lowerBound'+1


	// Drop the relative year column for ease of use in the event study.
	qui drop boundRelYear`relIndex'


	local missingDataChar = uchar(8224)
	
	if `upperBound'==6{
		local plotFormat = `"vertical yline(0) keep(*shiftedBoundRelYear*) drop(1.shiftedBoundRelYear 13.shiftedBoundRelYear) omitted baselevels xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1" 6 "0" 7 "1" 8 "2" 9 "3" 10 "4" 11 "5", angle(0)) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total Registrations" "relative to {&tau}=-1")"'
		

		
		local plotTitle = "Event Study Relative Year Fixed Effects"
		local plotSubtitle = "(±`upperBound' Years)"
		local plotNote = "-Point estimates and 95% confidence intervals shown"
		
		label define shiftedBoundRelYear 1 "t $\leq$ -6" 2 "t=-5" 3 "t=-4" 4 "t=-3" 5 "t=-2" 6 "t=-1" 7 "t=0" 8 "t=1" 9 "t=2" 10 "t=3" 11 "t=4" 12 "t=5" 13 "t $\geq$ 6"
		label values shiftedBoundRelYear shiftedBoundRelYear
		local plotNote2 = ""
		local es_controls = "boundRelYear1 boundRelYear2 boundRelYear3 boundRelYear4 boundRelYear5 boundRelYear7 boundRelYear8 boundRelYear9 boundRelYear10 boundRelYear11 boundRelYear12 boundRelYear13"
		local plot_order = "boundRelYear2_res boundRelYear3_res boundRelYear4_res boundRelYear5_res . boundRelYear7_res boundRelYear8_res boundRelYear9_res boundRelYear10 boundRelYear11_res boundRelYear12_res"
		mat at = [-5, -4, -3, -2, 0, 1, 2, 3, 4, 5, 10]
		local plotFormat = `"vertical yline(0) keep(*boundRelYear*) drop(boundRelYear1_res boundRelYear13_res) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total Registrations" "relative to {&tau}=-1") xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1" 6 "0" 7 "1" 8 "2" 9 "3" 10 "4" 11 "5", angle(0))"' //omitted baselevels xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1" 6 "0" 7 "1" 8 "2" 9 "3" 10 "4" 11 "5", angle(0))
		gen tempx = 5
		gen tempy = 0
		local max_ind = 13
	}
	else{
		local plotFormat = `"vertical yline(0) keep(*shiftedBoundRelYear*) drop(1.shiftedBoundRelYear 23.shiftedBoundRelYear) omitted baselevels xlabel(1 "-10{superscript:`missingDataChar'}" 2 "-9{superscript:`missingDataChar'}" 3 "-8{superscript:`missingDataChar'}" 4 "-7" 5 "-6" 6 "-5" 7 "-4" 8 "-3" 9 "-2" 10 "-1" 11 "0" 12 "1" 13 "2" 14 "3" 15 "4" 16 "5" 17 "6" 18 "7" 19 "8{superscript:`missingDataChar'}" 20 "9{superscript:`missingDataChar'}" 21 "10{superscript:`missingDataChar'}", angle(0) labsize(small)) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total Registrations" "relative to {&tau}=-1")"'
		
		
		local plotTitle = ""
		local plotSubtitle = "(±`upperBound' Years)"
		local plotNote = "Point estimates and 95% confidence intervals shown"
		
		label define shiftedBoundRelYear 1 "t $\leq$ -11" 2 "t=-10" 3 "t=-9" 4 "t=-8" 5 "t=-7" 6 "t=-6" 7 "t=-5" 8 "t=-4" 9 "t=-3" 10 "t=-2" 11 "t=-1" 12 "t=0" 13 "t=1" 14 "t=2" 15 "t=3" 16 "t=4" 17 "t=5" 18 "t=6" 19 "t=7" 20 "t=8" 21 "t=9" 22 "t=10" 23 "t $\geq$ 11"
		label values shiftedBoundRelYear shiftedBoundRelYear
		local plotNote2 = "-`missingDataChar' denotes a relative year with >1 state missing data"
		local plot_order = "boundRelYear2_res boundRelYear3_res boundRelYear4_res boundRelYear5_res boundRelYear6_res boundRelYear7_res boundRelYear8_res boundRelYear9_res boundRelYear10_res . boundRelYear12_res boundRelYear13_res boundRelYear14_res boundRelYear15_res boundRelYear16_res boundRelYear17_res boundRelYear18_res boundRelYear19_res boundRelYear20_res boundRelYear21_res boundRelYear22_res"
		local es_controls = "boundRelYear1 boundRelYear2 boundRelYear3 boundRelYear4 boundRelYear5 boundRelYear6 boundRelYear7 boundRelYear8 boundRelYear9 boundRelYear10 boundRelYear12 boundRelYear13 boundRelYear14 boundRelYear15 boundRelYear16 boundRelYear17 boundRelYear18 boundRelYear19 boundRelYear20 boundRelYear21 boundRelYear22 boundRelYear23"
		
		local plotFormat = `"vertical yline(0) keep(*boundRelYear*) drop(boundRelYear1_res boundRelYear23_res) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total Registrations" "relative to {&tau}=-1") xlabel(1 "-10{superscript:`missingDataChar'}" 2 "-9{superscript:`missingDataChar'}" 3 "-8{superscript:`missingDataChar'}" 4 "-7" 5 "-6" 6 "-5" 7 "-4" 8 "-3" 9 "-2" 10 "-1" 11 "0" 12 "1" 13 "2" 14 "3" 15 "4" 16 "5" 17 "6" 18 "7" 19 "8{superscript:`missingDataChar'}" 20 "9{superscript:`missingDataChar'}" 21 "10{superscript:`missingDataChar'}", angle(0))"'
		
		gen tempx = 10 
		gen tempy = 0
		local max_ind = 23
	}
	
	

	//Preferred Models
	//Spec. 1
	regFWL logregistrations, `es_controls', `spec1Controls', `absorbVars', `clusterVar', _
	
	coefplot, `plotFormat' name("spec1WoTT`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'RegESSpec1WoTT`upperBound'.png", name("spec1WoTT`upperBound'") replace width(`widthPix') //title("No DSCs Spec.")
	eststo spec1
	
	//Spec. 2
	regFWL logregistrations, `es_controls', `spec2Controls', `absorbVars', `clusterVar', _
	
	coefplot, `plotFormat' name("spec2WoTT`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'RegESSpec2WoTT`upperBound'.png", name("spec2WoTT`upperBound'") replace width(`widthPix') //title("Documented DSCs Spec.")
	graph export "`plotSaveDir'RegESSpec2WoTT`upperBound'.pdf", name("spec2WoTT`upperBound'") replace
	eststo spec2
	
	//Spec. 3
	regFWL logregistrations, `es_controls', `spec3Controls', `absorbVars', `clusterVar', _
	
	coefplot, `plotFormat' name("spec3WoTT`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'RegESSpec3WoTT`upperBound'.png", name("spec3WoTT`upperBound'") replace width(`widthPix') // title("Suspected DSCs Spec.")
	eststo spec3
	//Spec. 4
	regFWL logregistrations, `es_controls', `spec4Controls', `absorbVars', `clusterVar', _
	
	coefplot, `plotFormat' name("spec4WoTT`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'RegESSpec4WoTT`upperBound'.png", name("spec4WoTT`upperBound'") replace width(`widthPix')  //title("Potential DSCs Spec.")
	eststo spec4
	
	
	
	//Store Table

	if `upperBound'==6{
		local title = "Registrations Event Study Results Small Window w/o Time Trends \label{tab:ESWoTT`upperBound'}"
	}
	else{
		local title = "Registrations Event Study Results Large Window w/o Time Trends \label{tab:ESWoTT`upperBound'}"
	}
	
	graph combine spec1WoTT`bound' spec2WoTT`bound' spec3WoTT`bound' spec4WoTT`bound', ycommon
	graph export "`plotSaveDir'RegESCombinedWoTT`upperBound'.png", replace width(`widthPix')

	esttab spec1 spec2 spec3 spec4 using "`tableSaveDir'RegESWoTT`upperBound'.tex", `fmtEventStudy' title(`title') replace
	
	restore
}
